package jmcnet.libcommun.junit;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.context.ApplicationContext;

public class SpringBaseTestCase extends TestCase {
	private Logger log = Logger.getLogger(SpringBaseTestCase.class);

	private String testApplicationContextFileName="testBeanRefFactory.xml";
	private String testApplicationContextBeanName="beanRefFactory";
	
	private ApplicationContext appContexte = null;
	
	public SpringBaseTestCase() {
		super();
	}
	
	public SpringBaseTestCase(String testApplicationContextFileName, String testApplicationContextBeanName) {
		super();
		this.testApplicationContextBeanName = testApplicationContextBeanName;
		this.testApplicationContextFileName = testApplicationContextFileName;
	}
	
	protected org.springframework.context.ApplicationContext getContexte() {
		if (appContexte == null) {
			log.info("Initialisation de spring avec le fichier : '"+testApplicationContextFileName+"' et le beanName suivant : '"+testApplicationContextBeanName+"'" );
			
			org.springframework.beans.factory.access.BeanFactoryLocator beanFactoryLocator =
				org.springframework.context.access.ContextSingletonBeanFactoryLocator.getInstance(
            		testApplicationContextFileName);
			BeanFactoryReference beanFactoryReference = beanFactoryLocator.useBeanFactory(testApplicationContextBeanName);
			appContexte =(org.springframework.context.ApplicationContext) beanFactoryReference.getFactory();
		}
        return appContexte;
	}	
}
